/*   1:    */ package com.rinsi.util;
/*   2:    */ 
/*   3:    */ import java.io.File;
/*   4:    */ import java.io.FileInputStream;
/*   5:    */ import java.io.FileOutputStream;
/*   6:    */ import java.io.FilenameFilter;
/*   7:    */ import java.io.IOException;
/*   8:    */ import java.util.ArrayList;
/*   9:    */ import java.util.HashMap;
/*  10:    */ import java.util.List;
/*  11:    */ import java.util.Map;
/*  12:    */ import org.apache.commons.logging.Log;
/*  13:    */ import org.apache.commons.logging.LogFactory;
/*  14:    */ import org.apache.tomcat.util.http.fileupload.FileUtils;
/*  15:    */ import org.springframework.web.multipart.MultipartFile;
/*  16:    */ 
/*  17:    */ public class FileUtil
/*  18:    */ {
/*  19: 31 */   private static final Log log = LogFactory.getLog(FileUtil.class);
/*  20:    */   
/*  21:    */   public static File createFile(String filePath, String fileName, byte[] inBytes)
/*  22:    */     throws IOException
/*  23:    */   {
/*  24: 38 */     File dirFile = new File(filePath);
/*  25: 40 */     if (!dirFile.exists()) {
/*  26: 42 */       dirFile.mkdirs();
/*  27:    */     }
/*  28: 45 */     File file = new File(fileName);
/*  29:    */     
/*  30: 47 */     File target = new File(filePath, CodeUtil.code(getBaseFileName(file)) + "." + getFileType(file));
/*  31:    */     
/*  32: 49 */     FileOutputStream fileOS = new FileOutputStream(target);
/*  33:    */     
/*  34: 51 */     fileOS.write(inBytes);
/*  35:    */     
/*  36: 53 */     fileOS.close();
/*  37:    */     
/*  38: 55 */     return target;
/*  39:    */   }
/*  40:    */   
/*  41:    */   public static boolean creareDirectory(String ObjectPath)
/*  42:    */   {
/*  43: 63 */     boolean flag = true;
/*  44:    */     
/*  45: 65 */     File filePath = new File(ObjectPath);
/*  46: 67 */     if (!filePath.exists())
/*  47:    */     {
/*  48: 69 */       filePath.mkdirs();
/*  49:    */       
/*  50: 71 */       flag = false;
/*  51:    */     }
/*  52: 74 */     return flag;
/*  53:    */   }
/*  54:    */   
/*  55:    */   public static void backupFile(String tempPath, String ObjectPath, String dirName)
/*  56:    */     throws Exception
/*  57:    */   {
/*  58:    */     String backupPath;
/*  59:    */     //String backupPath;
/*  60: 87 */     if (!tempPath.endsWith(File.separator)) {
/*  61: 89 */       backupPath = tempPath + File.separator + dirName;
/*  62:    */     } else {
/*  63: 93 */       backupPath = tempPath + dirName;
/*  64:    */     }
/*  65: 96 */     File backupFilePath = new File(backupPath);
/*  66: 98 */     if (!backupFilePath.exists()) {
/*  67:100 */       backupFilePath.mkdirs();
/*  68:    */     }
/*  69:103 */     File filePath = new File(ObjectPath);
/*  70:105 */     if (!filePath.exists())
/*  71:    */     {
/*  72:107 */       log.debug("目录不存在!");
/*  73:    */     }
/*  74:111 */     else if (filePath.isDirectory())
/*  75:    */     {
/*  76:113 */       File[] list = filePath.listFiles();
/*  77:115 */       if ((list != null) && (list.length != 0))
/*  78:    */       {
/*  79:117 */         copyFolder(ObjectPath, backupPath);
/*  80:119 */         for (int i = 0; i < list.length; i++) {
/*  81:121 */           list[i].delete();
/*  82:    */         }
/*  83:    */       }
/*  84:    */     }
/*  85:    */   }
/*  86:    */   
/*  87:    */   public static void recoverFile(String tempPath, String ObjectPath, String dirName)
/*  88:    */     throws Exception
/*  89:    */   {
/*  90:139 */     if (ObjectPath.endsWith(File.separator)) {
/*  91:141 */       ObjectPath = ObjectPath + dirName;
/*  92:    */     } else {
/*  93:145 */       ObjectPath = ObjectPath + File.separatorChar + dirName;
/*  94:    */     }
/*  95:    */     String backupPath;
/*  96:    */     //String backupPath;
/*  97:148 */     if (!tempPath.endsWith(File.separator)) {
/*  98:150 */       backupPath = tempPath + File.separator + dirName;
/*  99:    */     } else {
/* 100:154 */       backupPath = tempPath + dirName;
/* 101:    */     }
/* 102:157 */     File backupFilePath = new File(backupPath);
/* 103:159 */     if (!backupFilePath.exists()) {
/* 104:161 */       backupFilePath.mkdirs();
/* 105:    */     }
/* 106:164 */     File filePath = new File(ObjectPath);
/* 107:166 */     if (!filePath.exists())
/* 108:    */     {
/* 109:168 */       log.debug("目录不存在!");
/* 110:    */     }
/* 111:172 */     else if (filePath.isDirectory())
/* 112:    */     {
/* 113:174 */       File[] list = filePath.listFiles();
/* 114:176 */       if ((list != null) && (list.length != 0)) {
/* 115:178 */         copyFolder(backupPath, ObjectPath);
/* 116:    */       }
/* 117:    */     }
/* 118:    */   }
/* 119:    */   
/* 120:    */   public static void copyFolder(String oldPath, String newPath)
/* 121:    */     throws Exception
/* 122:    */   {
/* 123:194 */     File newFile = new File(newPath);
/* 124:196 */     if (!newFile.exists()) {
/* 125:198 */       newFile.mkdirs();
/* 126:    */     }
/* 127:201 */     File a = new File(oldPath);
/* 128:    */     
/* 129:203 */     String[] file = a.list();
/* 130:    */     
/* 131:205 */     File temp = null;
/* 132:207 */     for (int i = 0; i < file.length; i++)
/* 133:    */     {
/* 134:209 */       if (oldPath.endsWith(File.separator)) {
/* 135:211 */         temp = new File(oldPath + file[i]);
/* 136:    */       } else {
/* 137:215 */         temp = new File(oldPath + File.separator + file[i]);
/* 138:    */       }
/* 139:218 */       if (temp.isFile())
/* 140:    */       {
/* 141:220 */         FileInputStream input = new FileInputStream(temp);
/* 142:    */         
/* 143:222 */         FileOutputStream output = new FileOutputStream(newPath + "/" + temp.getName().toString());
/* 144:    */         
/* 145:224 */         byte[] b = new byte[5120];
/* 146:    */         int len;
/* 147:228 */         while ((len = input.read(b)) != -1)
/* 148:    */         {
/* 149:    */           //int len;
/* 150:230 */           output.write(b, 0, len);
/* 151:    */         }
/* 152:233 */         output.flush();
/* 153:    */         
/* 154:235 */         output.close();
/* 155:    */         
/* 156:237 */         input.close();
/* 157:    */       }
/* 158:239 */       if (temp.isDirectory()) {
/* 159:241 */         copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
/* 160:    */       }
/* 161:    */     }
/* 162:    */   }
/* 163:    */   
/* 164:    */   public static void deleteFileAndDirectory(String tempPath, String dirName)
/* 165:    */   {
/* 166:    */     String ObjectPath;
/* 167:    */     //String ObjectPath;
/* 168:258 */     if (!tempPath.endsWith(File.separator)) {
/* 169:260 */       ObjectPath = tempPath + File.separator + dirName;
/* 170:    */     } else {
/* 171:264 */       ObjectPath = tempPath + dirName;
/* 172:    */     }
/* 173:267 */     File filePath = new File(ObjectPath);
/* 174:269 */     if (!filePath.exists())
/* 175:    */     {
/* 176:271 */       filePath.mkdirs();
/* 177:    */       
/* 178:273 */       log.debug("目录不存在!");
/* 179:    */     }
/* 180:    */     else
/* 181:    */     {
/* 182:276 */       if (filePath.isDirectory())
/* 183:    */       {
/* 184:278 */         File[] list = filePath.listFiles();
/* 185:280 */         for (int i = 0; i < list.length; i++) {
/* 186:282 */           list[i].delete();
/* 187:    */         }
/* 188:    */       }
/* 189:286 */       filePath.delete();
/* 190:    */     }
/* 191:    */   }
/* 192:    */   
/* 193:    */   public static boolean existFileInDirectory(String ObjectPath)
/* 194:    */   {
/* 195:299 */     boolean flag = false;
/* 196:    */     
/* 197:301 */     File filePath = new File(ObjectPath);
/* 198:303 */     if (filePath.exists()) {
/* 199:305 */       if (filePath.isDirectory())
/* 200:    */       {
/* 201:307 */         File[] list = filePath.listFiles();
/* 202:309 */         if ((list != null) && (list.length != 0)) {
/* 203:311 */           flag = true;
/* 204:    */         }
/* 205:    */       }
/* 206:    */     }
/* 207:316 */     return flag;
/* 208:    */   }
/* 209:    */   
/* 210:    */   public static void deleteDirectory(String ObjectPath)
/* 211:    */     throws IOException
/* 212:    */   {
/* 213:327 */     File filePath = new File(ObjectPath);
/* 214:329 */     if (filePath.exists()) {
/* 215:331 */       FileUtils.deleteDirectory(filePath);
/* 216:    */     }
/* 217:    */   }
/* 218:    */   
/* 219:    */   public static boolean deleteExistedFile(String ObjectPath, String fileName)
/* 220:    */   {
/* 221:343 */     boolean flag = false;
/* 222:    */     
/* 223:345 */     File filePath = new File(ObjectPath);
/* 224:347 */     if (filePath.exists()) {
/* 225:349 */       if (filePath.isDirectory())
/* 226:    */       {
/* 227:351 */         File[] list = filePath.listFiles(new FilenameFilter()
/* 228:    */         {
/* 229:    */           public boolean accept(File dir, String name)
/* 230:    */           {
/* 231:355 */             File file = null;//new File(FileUtil.this);
/* 232:    */             
/* 233:357 */             return name.equals(CodeUtil.code(FileUtil.getBaseFileName(file)) + "." + FileUtil.getFileType(file));
/* 234:    */           }
/* 235:    */         });
/* 236:361 */         for (int i = 0; i < list.length; i++) {
/* 237:362 */           list[i].delete();
/* 238:    */         }
/* 239:364 */         flag = true;
/* 240:    */       }
/* 241:    */     }
/* 242:368 */     return flag;
/* 243:    */   }
/* 244:    */   
/* 245:    */   public static List<Map<String, Object>> getFileList(String path)
/* 246:    */   {
/* 247:376 */     File directory = new File(path);
/* 248:    */     
/* 249:378 */     List<Map<String, Object>> rsFlies = new ArrayList();
/* 250:    */     
/* 251:380 */     File[] files = directory.listFiles();
/* 252:382 */     if (files == null) {
/* 253:382 */       return rsFlies;
/* 254:    */     }
/* 255:384 */     for (int i = 0; i < files.length; i++)
/* 256:    */     {
/* 257:386 */       Map<String, Object> map = new HashMap();
/* 258:    */       
/* 259:388 */       map.put("filename", CodeUtil.decode(getBaseFileName(files[i])));
/* 260:    */       
/* 261:390 */       map.put("filetype", getFileType(files[i]));
/* 262:    */       
/* 263:392 */       rsFlies.add(map);
/* 264:    */     }
/* 265:395 */     return rsFlies;
/* 266:    */   }
/* 267:    */   
/* 268:    */   public static File getByName(String path, String fileName)
/* 269:    */   {
/* 270:403 */     File directory = new File(path);
/* 271:    */     
/* 272:405 */     File[] files = directory.listFiles();
/* 273:407 */     for (int i = 0; i < files.length; i++) {
/* 274:409 */       if (fileName.equals(getBaseFileName(files[i]))) {
/* 275:411 */         return files[i];
/* 276:    */       }
/* 277:    */     }
/* 278:415 */     return null;
/* 279:    */   }
/* 280:    */   
/* 281:    */   public static File getOneFile(String path)
/* 282:    */   {
/* 283:422 */     File directory = new File(path);
/* 284:    */     
/* 285:424 */     File[] files = directory.listFiles();
/* 286:426 */     if (files == null) {
/* 287:426 */       return null;
/* 288:    */     }
/* 289:428 */     return files[0];
/* 290:    */   }
/* 291:    */   
/* 292:    */   public static String getBaseFileName(File file)
/* 293:    */   {
/* 294:437 */     if (file == null) {
/* 295:437 */       return null;
/* 296:    */     }
/* 297:439 */     return file.getName().replaceAll("[.][^.]+$", "");
/* 298:    */   }
/* 299:    */   
/* 300:    */   public static String getFileType(File file)
/* 301:    */   {
/* 302:447 */     if (file == null) {
/* 303:447 */       return null;
/* 304:    */     }
/* 305:449 */     return file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());
/* 306:    */   }
/* 307:    */   
/* 308:    */   public static String getFileType(MultipartFile file)
/* 309:    */   {
/* 310:458 */     if (file == null) {
/* 311:458 */       return null;
/* 312:    */     }
/* 313:460 */     return file.getOriginalFilename().replaceAll(getBaseFileName(file) + ".", "");
/* 314:    */   }
/* 315:    */   
/* 316:    */   public static String getBaseFileName(MultipartFile file)
/* 317:    */   {
/* 318:469 */     if (file == null) {
/* 319:469 */       return null;
/* 320:    */     }
/* 321:471 */     return file.getOriginalFilename().replaceAll("[.][^.]+$", "");
/* 322:    */   }
/* 323:    */ }


/* Location:           D:\Users\Desktop\shanxi\项目\sxazmis\WEB-INF\classes\
 * Qualified Name:     com.rinsi.util.FileUtil
 * JD-Core Version:    0.7.0.1
 */